#pragma once
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include <cerrno>
#include <netinet/in.h>
#include <sstream>
#include <arpa/inet.h>
#include <pthread.h>
#include <functional>
#include <vector>
#include <signal.h>
#include "logMessage.hpp"
#include "Unit.hpp"
#define SEP " "
#define SEP_LEN strlen(SEP)
#define LINE_SEP "\r\n"
#define LINE_LEN strlen(LINE_SEP)
using namespace std;
// 输入型
const string default_path = "wwwroot";
const string home_path = "index.html";
const string html_404 = "wwwroot/404.html";
class httpRequest
{
public:
    void parse()
    {
        //拿到第一行，GET url version
        string Line = Unit::Getline(inbuffer,"\r\n");
        if(Line.empty())
        {
            cout<<"Line is empty"<<endl;
            return;
        }
        cout<<"Line : "<<Line<<endl;
        stringstream ss(Line);
        ss>>method>>url>>httpVersion;
        //拿到下面的正文
        path = default_path;
        path += url;
        if(path[path.size()-1] == '/')
        path+=home_path;
        //获取后缀
        auto pos = path.rfind(".");
        if(pos == string::npos)
        {
            suffix = ".html";
        }
        suffix = path.substr(pos);

        //获得资源的大小
        struct stat st;
        int n = stat(path.c_str(),&st);
        if(n==0) size = st.st_size;
        else size = -1;
    }
public:
    string inbuffer;
    // string reqline;
    // vector<string> readHeader;
    // string body;
    string method;
    string url;
    string httpVersion;
    string path;
    string suffix;
    int size;
};
class httpRespons
{
public:
    string outbuffer;
};
